package com.one.rope.mvp.web;

import com.one.rope.mvp.web.business.service.ICommonDataService;
import com.one.rope.mvp.web.common.cache.GlobalCacheDataObject;
import com.one.rope.mvp.web.common.redis.RedisClientUtil;
import com.one.rope.mvp.web.config.BuildConfig;
import com.one.rope.mvp.web.config.CommonConfig;
import com.one.rope.mvp.web.config.WebInterceptorConfig;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@EnableTransactionManagement
@SpringBootApplication
@MapperScan(basePackages = {"com.one.rope.mvp.web.business.dao"})
@EnableConfigurationProperties({CommonConfig.class, WebInterceptorConfig.class})
@ComponentScan(basePackages = {"com.one.rope.mvp"})
@Slf4j
public class OneRopeMvpWebApplication {

  @Value("${server.port}")
  private String port;

  @Value("${spring.datasource.url}")
  private String dataSourceUrl;

  @Autowired
  private DataSource dataSource;

  @Autowired
  private Environment environment;

  @Autowired
  private CommonConfig commonConfig;

  @Autowired
  private RedisClientUtil redisClientUtil;

  @Autowired
  ICommonDataService commonDataService;

  public static void main(String[] args) {
    SpringApplication.run(OneRopeMvpWebApplication.class, args);
  }

  /**
   * 打印当前启动配置
   */
  private void getActiveProfiles() {
    log.warn("******加载配置, MYSQL:" + dataSourceUrl + "," + dataSource.toString() + "\n");
    log.warn("redis:" + commonConfig.getHost() + ":" + commonConfig.getPort() + "\n");
    log.warn("max Membory:" + Runtime.getRuntime().maxMemory() + "\n");
    log.warn("OneRopeMvpWebApplication start success on port: " + port);
  }

  @PostConstruct
  private void init() {
    // 初始化环境配置
    BuildConfig.initWithEnvid(environment);
    redisClientUtil.init(commonConfig);
    // 初始化缓存对象
    GlobalCacheDataObject.getInstance().initCacheData(commonDataService);
    getActiveProfiles();
  }

}
